package Demo1;

import java.util.*;
class UF{
    public int[] array;
    public UF(int n){
        array = new int[n];
        Arrays.fill(array,-1);
    }

    public int find(int x){
        while (array[x] >= 0) {
            x = array[x];
        }
        return x;
    }

    public void merge(int x,int y){
        int rootx = find(x);
        int rooty = find(y);
        if(rootx!=rooty){
            if(array[rootx]<=array[rooty]){
                array[rooty] = rootx;
                array[rootx] +=array[rooty];
            }else{
                array[rootx] = rooty;
                array[rooty] +=array[rootx];
            }
        }
    }

    public int getNum(){
        int sum = 0;
        for(int number : array){
            if(number<0) sum++;
        }
        return sum;
    }

}

public class demo1 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int m =in.nextInt(),n = in.nextInt();
        UF uf = new UF(m*n);
        int k = in.nextInt();
        for(int i = 0;i<k;i++){
            uf.merge(in.nextInt()-1,in.nextInt()-1);
        }
        System.out.println(uf.getNum());
        in.close();
    }
}
